//
//  WCNTimesWeekDayVC.m
//  WCN
//
//  Created by 杨鑫 on 15/6/9.
//  Copyright (c) 2015年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCNTimesWeekDayVC.h"
//
#import "WCNSetTimeCCell.h"
#import "WCNSetTimeHeaderCRV.h"
//
#import "Macros.h"

static NSString *cellId = @"WCNSetTimeCCell";
static NSString *headerId = @"WCNSetTimeHeaderCRV";
static NSString *footerId = @"UICollectionReusableView";

@interface WCNTimesWeekDayVC () <UICollectionViewDataSource, UICollectionViewDelegate>

@end

@implementation WCNTimesWeekDayVC

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super init];
    if (self) {
        _selectedTimes = [NSMutableArray array];
        
        //init UICollectionViewFlowLayout
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        flowLayout.minimumLineSpacing = 0;
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.itemSize = CGSizeMake((frame.size.width-2.0f)/3.0f, 50);
        flowLayout.headerReferenceSize = CGSizeMake(frame.size.width, 33);
        flowLayout.footerReferenceSize = CGSizeMake(frame.size.width, kOnePX);
        
        //init UICollectionView
        UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:flowLayout];
        collectionView.dataSource = self;
        collectionView.delegate = self;
        collectionView.showsVerticalScrollIndicator = NO;
        collectionView.showsHorizontalScrollIndicator = NO;
        collectionView.backgroundColor = [UIColor whiteColor];
        
        [collectionView registerNib:[UINib nibWithNibName:cellId bundle:nil] forCellWithReuseIdentifier:cellId];
        [collectionView registerNib:[UINib nibWithNibName:headerId bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerId];
        [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerId];
        
        collectionView.allowsMultipleSelection = YES;
        
        _collectionView = collectionView;
    }
    return self;
}

- (BOOL)cellIsSelected:(NSInteger)timeId
{
    for (NSDictionary *time in self.selectedTimes) {
        if ([time[@"timePeriodId"] integerValue] == timeId) {
            return YES;
        }
    }
    
    return NO;
}

#pragma mark - <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.data[section] count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *item = [self.data[indexPath.section] objectAtIndex:indexPath.row];
    BOOL selected = [self cellIsSelected:[item[@"id"] integerValue]];
    
    WCNSetTimeCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    cell.timeLabel.text = item[@"timePeriod"];
    if (selected) {
        [collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
        [cell setSelected:YES];
    } else {
        [collectionView deselectItemAtIndexPath:indexPath animated:NO];
        [cell setSelected:NO];
    }
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        WCNSetTimeHeaderCRV *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerId forIndexPath:indexPath];
        if (indexPath.section == 0) {
            header.timeLabel.text = @"上午";
        } else {
            header.timeLabel.text = @"下午";
        }
        
        return header;
    } else if (kind == UICollectionElementKindSectionFooter) {
        UICollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:footerId forIndexPath:indexPath];
        footer.backgroundColor = kBorderColor;
        return footer;
    }
    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *item = [self.data[indexPath.section] objectAtIndex:indexPath.row];
    [self.selectedTimes addObject:@{@"weekDayId":self.weekData[@"id"], @"timePeriodId":item[@"id"]}];
}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *item = [self.data[indexPath.section] objectAtIndex:indexPath.row];
    for (NSDictionary *time in self.selectedTimes) {
        if ([time[@"timePeriodId"] integerValue] == [item[@"id"] integerValue]) {
            [self.selectedTimes removeObjectIdenticalTo:time];
            break;
        }
    }
}

@end
